$(document).ready(
    function() {
        obtainType();
    });

/*验证license名称*/
jQuery.validator.addMethod("licenseName",function (value, element) {
    var regex =  /^\+?[0-9]\d*$/;
    return this.optional(element)  || regex.test(value);
},"license名称必须是英文加字母组合！");


function obtainType(){
    $.ajax({
        url:'../license/license_licenseTemplate',
        async: false,
        dataType: "json",
        success:function(data) {
            con = "";
            for (var i in data) {
                con += "<option value="+data[i].id+">"+data[i].type+"</option>";
                $("#licenseVersion").html(con);
            }
        }
    });
}

function f_verfy() {
    var file = $("#licFile").val();
    var edition = $("#licenseVersion").find("option:selected").text();
    var subject = $("#licenseName").val();

    if (!f_check_param(file, edition, subject)) return;

    var formData = new FormData();
    formData.append("licFile", document.getElementById("licFile").files[0]);
    formData.append("subject", subject);
    formData.append("edition", edition);

    $.ajax({
        url:'../license/verfy',
        type: 'POST',
        data: formData,
        contentType: false,
        processData: false,
        async: false,
        cache: false,
        success:function(data) {
           var result = JSON.parse(data);;

           if (result.isSuccess) {
               f_init_info(result.model);
           } else {
               alert("验证失败!");
               $("#license-info").hide();
           }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
}

function f_check_param(file, edition, licenseName) {
    if (file == "") {
        alert("请选择文件!");
        return false;
    }

    var index1=file.lastIndexOf(".");
    var index2=file.length;
    var suffix=file.substring(index1+1,index2);

   if (suffix != "lic") {
        alert("请选择后缀名为lic文件!");
        return false;
   }

   if (!licenseName) {
        alert("请输入license名称!");
        return false;
   }

   if (!edition) {
        alert("请选中license版本!");
        return false;
   }

   return true;
}

function f_init_info(model) {
    $("#license-info").show();

    $("#lic-name").html(model.subject);
    $("#lic-edition").html(model.edition);
    $("#lic-machines").html(model.macheinesNums);
    $("#lic-start").html(model.notBefore);
    $("#lic-end").html(model.notAfter);
    $("#lic-info").html(model.info);
    $("#lic-ip").html(model.baseComputerModel.ipAddress);
    $("#lic-disk").html(model.baseComputerModel.hardDiskSN);
    $("#lic-mac").html(model.baseComputerModel.ipMacAddress);
    $("#lic-board").html(model.baseComputerModel.motherboardSN);
    $("#lic-system").html(model.baseComputerModel.systemSN);
    $("#lic-cpu").html(model.baseComputerModel.cPUSerial);
}


